package com.leetcode.partition21;

import java.io.*;

/**
 * 00953e0edea8_
 * @author `RKC`
 * @date 2022/3/29 9:04
 */
public class LC2024考试的最大困扰度 {

    private static final int N = 50010;
    private static int n;
    private static char[] t;
    private static int[] sumt = new int[N], sumf = new int[N];

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] ss = reader.readLine().split(" ");
        writer.write(maxConsecutiveAnswers(ss[0], Integer.parseInt(ss[1])) + "\n");
        writer.flush();
    }

    public static int maxConsecutiveAnswers(String answerKey, int k) {
        //当k最大为n时，答案必定为n，当k为0时，结果就是最长连续，通过进一步分析可以看出具有二段性，考虑使用二分
        t = answerKey.toCharArray();
        n = answerKey.length();
        //预处理出前缀和
        for (int i = 1; i <= n; i++) {
            if (t[i - 1] == 'T') {
                sumt[i] += sumt[i - 1] + 1;
                sumf[i] = sumf[i - 1];
            } else {
                sumf[i] += sumf[i - 1] + 1;
                sumt[i] = sumt[i - 1];
            }
        }
        int l = 1, r = answerKey.length();
        while (l < r) {
            int mid = l + r + 1 >> 1;
            if (check(mid, k)) l = mid;
            else r = mid - 1;
        }
        return l;
    }

    private static boolean check(int mid, int k) {
        //检测在操作不超过k次的情况下，是否能至少存在一个方案使得有连续长为mid的T出现
        for (int i = 0; i <= n - mid; i++) {
            //只在[i,i+mid-1]范围内出现的次数
            int cntt = sum(sumt, i + 1, i + mid), cntf = sum(sumf, i + 1, i + mid);
            if (cntt > cntf && cntf <= k) return true;
            if (cntf >= cntt && cntt <= k) return true;
        }
        return false;
    }

    private static int sum(int[] sum, int l, int r) {
        return sum[r] - sum[l - 1];
    }
}
